<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车牌" prop="carNumber">
        <el-input
          v-model="queryParams.carNumber"
          placeholder="请输入车牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年检状态" prop="inspectionStatus">
        <el-select v-model="queryParams.inspectionStatus" placeholder="请选择年检状态" clearable>
          <el-option
            v-for="dict in dict.type.inspection_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="年检年限" prop="inspectionYear">
        <el-input
          v-model="queryParams.inspectionYear"
          placeholder="请输入年检年限"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仪表盘检查" prop="inspectionDashboard">
        <el-select v-model="queryParams.inspectionDashboard" placeholder="请选择仪表盘检查" clearable>
          <el-option
            v-for="dict in dict.type.inspection_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="刹车检查" prop="inspectionBrake">
        <el-select v-model="queryParams.inspectionBrake" placeholder="请选择刹车检查" clearable>
          <el-option
            v-for="dict in dict.type.inspection_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发动机检查" prop="inspectionEngine">
        <el-select v-model="queryParams.inspectionEngine" placeholder="请选择发动机检查" clearable>
          <el-option
            v-for="dict in dict.type.inspection_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="轮胎检查" prop="inspectionTyre">
        <el-select v-model="queryParams.inspectionTyre" placeholder="请选择轮胎检查" clearable>
          <el-option
            v-for="dict in dict.type.inspection_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['car:inspection:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['car:inspection:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['car:inspection:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['car:inspection:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inspectionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="inspectionId" />
      <el-table-column label="车牌" align="center" prop="carNumber" />
      <el-table-column label="年检状态" align="center" prop="inspectionStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.inspection_status" :value="scope.row.inspectionStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="年检年限" align="center" prop="inspectionYear" />
      <el-table-column label="仪表盘检查" align="center" prop="inspectionDashboard">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.inspection_status" :value="scope.row.inspectionDashboard"/>
        </template>
      </el-table-column>
      <el-table-column label="刹车检查" align="center" prop="inspectionBrake">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.inspection_status" :value="scope.row.inspectionBrake"/>
        </template>
      </el-table-column>
      <el-table-column label="发动机检查" align="center" prop="inspectionEngine">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.inspection_status" :value="scope.row.inspectionEngine"/>
        </template>
      </el-table-column>
      <el-table-column label="轮胎检查" align="center" prop="inspectionTyre">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.inspection_status" :value="scope.row.inspectionTyre"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['car:inspection:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['car:inspection:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改年检对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="车牌" prop="carNumber">
          <el-input v-model="form.carNumber" placeholder="请输入车牌" />
        </el-form-item>
        <el-form-item label="年检状态">
          <el-radio-group v-model="form.inspectionStatus">
            <el-radio
              v-for="dict in dict.type.inspection_status"
              :key="dict.value"
:label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="年检年限" prop="inspectionYear">
          <el-input v-model="form.inspectionYear" placeholder="请输入年检年限" />
        </el-form-item>
        <el-form-item label="仪表盘检查">
          <el-radio-group v-model="form.inspectionDashboard">
            <el-radio
              v-for="dict in dict.type.inspection_status"
              :key="dict.value"
:label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="刹车检查">
          <el-radio-group v-model="form.inspectionBrake">
            <el-radio
              v-for="dict in dict.type.inspection_status"
              :key="dict.value"
:label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="发动机检查">
          <el-radio-group v-model="form.inspectionEngine">
            <el-radio
              v-for="dict in dict.type.inspection_status"
              :key="dict.value"
:label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="轮胎检查">
          <el-radio-group v-model="form.inspectionTyre">
            <el-radio
              v-for="dict in dict.type.inspection_status"
              :key="dict.value"
:label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInspection, getInspection, delInspection, addInspection, updateInspection } from "@/api/car/inspection";

export default {
  name: "Inspection",
  dicts: ['inspection_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 年检表格数据
      inspectionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carNumber: null,
        inspectionStatus: null,
        inspectionYear: null,
        inspectionDashboard: null,
        inspectionBrake: null,
        inspectionEngine: null,
        inspectionTyre: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        carNumber: [
          { required: true, message: "车牌不能为空", trigger: "blur" }
        ],
        inspectionStatus: [
          { required: true, message: "年检状态不能为空", trigger: "blur" }
        ],
        inspectionYear: [
          { required: true, message: "年检年限不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询年检列表 */
    getList() {
      this.loading = true;
      listInspection(this.queryParams).then(response => {
        this.inspectionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        inspectionId: null,
        carNumber: null,
        inspectionStatus: "0",
        inspectionYear: null,
        inspectionDashboard: "0",
        inspectionBrake: "0",
        inspectionEngine: "0",
        inspectionTyre: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.inspectionId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加年检";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const inspectionId = row.inspectionId || this.ids
      getInspection(inspectionId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改年检";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.inspectionId != null) {
            updateInspection(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInspection(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const inspectionIds = row.inspectionId || this.ids;
      this.$modal.confirm('是否确认删除年检编号为"' + inspectionIds + '"的数据项？').then(function() {
        return delInspection(inspectionIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('car/inspection/export', {
        ...this.queryParams
      }, `inspection_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
